<?php
class tipo extends Controller {  
  
	function tipo() {
		parent::Controller();
		$this->load->library("rapyd");
		//$this->datasis->modulo_id(100,1);
	}  
	function index(){
		$this->rapyd->load("datagrid2");	  
		$this->rapyd->load("dataform");
		$this->load->helper('openflash');
				
		$grid = new DataGrid2();
		
		$select=array("a.tipcod","b.tip_descrip","COUNT(*) as grantotal");
		     		
		$grid->db->select($select);  
		$grid->db->from("scli as a");
		$grid->db->join("tipo as b","a.tipcod=b.tip_cod");
		$grid->db->groupby('a.tipcod'); 
		$grid->db->orderby('grantotal DESC');
		
		$grid->column("Tipo" , "tip_descrip","align='left'");          
		$grid->column("Cantidad", "grantotal",'align=right');
		
		$grid->totalizar('grantotal');
		$grid->build();
				
		$grafico = open_flash_chart_object(600,300, site_url("estadisticas/tipo/gtipo"));
		
		$data['content'] = $grafico;
		$data['content'] .= $grid->output;

		$data["head"]    = script("jquery.pack.js").script("plugins/jquery.numeric.pack.js").script("plugins/jquery.floatnumber.js").$this->rapyd->get_head();
		$data['filtro'] =' ';
		$data['title']    = $this->rapyd->get_head()."<h1>Tipos de Clientes</h1>";
		$this->load->view('view_ventanas_sola', $data);
	}
	function gtipo(){
		$this->load->library('Graph');
		
		$mSQL = "SELECT CONCAT(b.tip_descrip,'(',COUNT(*),')')AS tip_descrip,tip_descrip as descrip,COUNT(*) as grantotal,a.tipcod
		FROM scli as a
		LEFT JOIN tipo as b ON a.tipcod=b.tip_cod
		GROUP BY a.tipcod"; 
						
		//echo $mSQL;
		   	
		$maxval=0;
		$query=$this->db->query($mSQL);
		
		$data=array();
		$i=0;
		$total=0;
		foreach($query->result() as $row ){
		if ($row->grantotal>$maxval)
			$maxval=$row->grantotal;
		 	$tipcod[]=$row->tipcod;
		 	$tip_descrip[]=$row->tip_descrip;
		 	$data[]=$row->grantotal;
		 	$total+=$row->grantotal;
		}
								
		$bar = new bar_3d( 75, '#14A989' );
		//$bar_red->key( $tip_descrip, 10 );
		  
		for($i=0;$i<count($data);$i++ ){
			$bar->add_data_tip($data[$i], graph::esc( number_format($data[$i],2,',','.')));
		} 
				
		$g = new graph();
		$g->title( ' ', '{font-size:20px; color: #0D358A; margin: 5px; padding:5px; padding-left: 20px; padding-right: 20px;}' );
			
		$g->data_sets[] = $bar;
		
		$g->set_x_axis_3d( 12 );
		$g->x_axis_colour( '#0F4481', '#ADB5C7' );
		
		$g->set_x_labels($tip_descrip);
		$g->set_x_legend('Total de Clientes '.number_format($total,0,'','.'), 14, '#004381' );
		$g->set_y_max($total);  
		$g->y_label_steps( 5 );
		$g->set_y_legend( 'Fyco Telecomunicaciones', 12, '#0D358A' );
		echo $g->render();
		
	}
}
?>